
layui.use(["element", "jquery", "table", "form", "laydate", "okLayer", "okUtils", "okMock"], function () {
    let table = layui.table;
    let form = layui.form;
    let laydate = layui.laydate;
    let okLayer = layui.okLayer;
    let okUtils = layui.okUtils;
    let okMock = layui.okMock;
    let $ = layui.jquery;

    okLoading.close($);

    laydate.render({elem: "#startTime", type: "datetime"});
    laydate.render({elem: "#endTime", type: "datetime"});

    let userTable = table.render({
        elem: '#tableId',
        url: ctx +"/user/pageList",
        method:'post',
        contentType: 'application/json',
        dataType: 'json',
        limit: 10,
        page: true,
        toolbar: true,
        toolbar: "#toolbarTpl",
        size: "sm",
        where:{//其他参数
        },
        request: {
            pageName: 'pageNum', //页码的参数名称，默认：page
            limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        response: {
            statusName: 'code', //规定数据状态的字段名称，默认：code
            statusCode: 0, //规定成功的状态码，默认：0
            msgName: 'hint', //规定状态信息的字段名称，默认：msg
            countName: 'count', //规定数据总数的字段名称，默认：count
            dataName: 'data' //规定数据列表的字段名称，默认：data
        },
        parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code , //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        },
        cols: [[
            {type: "checkbox", fixed: "left"},
            {field: "id", title: "ID", width: 160, sort: true},
            {field: "account", title: "账号", width: 100},
            {field: "nickName", title: "昵称", width: 100},
            {field: "mobile", title: "手机号", width: 120},
            {field: "email", title: "邮箱", width: 200},
            {field: "type", title: "账号类型", width: 100},
            {field: "status", title: "状态", width: 100, templet: "#statusTpl"},
            // {field: "role", title: "角色", width: 100, templet: "#roleTpl"},
            {title: "操作", width: 100, align: "center", fixed: "right", templet: "#operationTpl"}
        ]],
        done: function (res, curr, count) {
            console.info(res, curr, count);
        }
    });

    form.on("submit(search)", function (data) {
        userTable.reload({
            where: {
                "account": data.field.account,
                "createAtMax": "" == data.field.createAtMax ? null:data.field.createAtMax,
                "createAtMin": "" == data.field.createAtMin ? null:data.field.createAtMin,
                "email": data.field.email,
                "nickName": data.field.nickName,
                "status": data.field.status
            },
            page: {curr: 1}
        });
        return false;
    });

    table.on("toolbar(tableFilter)", function (obj) {
        switch (obj.event) {
            case "batchEnabled":
                batchEnabled();
                break;
            case "batchDisabled":
                batchDisabled();
                break;
            case "batchDel":
                batchDel();
                break;
            case "add":
                add();
                break;
        }
    });

    table.on("tool(tableFilter)", function (obj) {
        let data = obj.data;
        switch (obj.event) {
            case "edit":
                edit(data);
                break;
            case "del":
                del(data.id);
                break;
        }
    });

    function batchEnabled() {
        okLayer.confirm("确定要批量启用吗？", function (index) {
            layer.close(index);
            let idsStr = okUtils.tableBatchCheck(table);
            if (idsStr) {
                okUtils.ajax("/user/normalUser", "put", {idsStr: idsStr}, true).done(function (response) {
                    console.log(response);
                    okUtils.tableSuccessMsg(response.msg);
                }).fail(function (error) {
                    console.log(error)
                });
            }
        });
    }

    function batchDisabled() {
        okLayer.confirm("确定要批量停用吗？", function (index) {
            layer.close(index);
            let idsStr = okUtils.tableBatchCheck(table);
            if (idsStr) {
                okUtils.ajax("/user/stopUser", "put", {idsStr: idsStr}, true).done(function (response) {
                    console.log(response);
                    okUtils.tableSuccessMsg(response.msg);
                }).fail(function (error) {
                    console.log(error)
                });
            }
        });
    }

    function batchDel() {
        okLayer.confirm("确定要批量删除吗？", function (index) {
            layer.close(index);
            let idsStr = okUtils.tableBatchCheck(table);
            if (idsStr) {
                okUtils.ajax("/user/deleteUser", "delete", {idsStr: idsStr}, true).done(function (response) {
                    console.log(response);
                    okUtils.tableSuccessMsg(response.msg);
                }).fail(function (error) {
                    console.log(error)
                });
            }
        });
    }

    function add() {
        okLayer.open("添加用户", "user-insert", "90%", "90%", null, function () {
            userTable.reload();
        })
    }

    function edit(data) {
        okLayer.open("更新用户", "user-update", "90%", "90%", function (layero) {
            let iframeWin = window[layero.find("iframe")[0]["name"]];
            iframeWin.initForm(data);
        }, function () {
            userTable.reload();
        })
    }

    function del(id) {
        okLayer.confirm("确定要删除吗？", function () {
            okUtils.ajax("/user/deleteUser", "delete", {idsStr: id}, true).done(function (response) {
                console.log(response);
                okUtils.tableSuccessMsg(response.msg);
            }).fail(function (error) {
                console.log(error)
            });
        })
    }
})